(function(){
    angular.module('feibo.teacher.controller').controller('BigClassOptController', BigClassOptController);

    BigClassOptController.$inject = ['$stateParams', '$state', 'TeacherService', 'valiFrom', 'kvMap', 'CommonService'];

    function BigClassOptController($stateParams, $state, TeacherService, valiFrom, kvMap, CommonService){
        var vm = this;

        vm.kvMap = {
            shiftCategoryTimeSlot: CommonService.cloneMapping(kvMap.shiftCategoryTimeSlot, ['vkMap'])
        };

        vm.data = {
            classId: $stateParams.id,
            isCheck: !$stateParams.edit,
            formData: {
                name: undefined,  // 班型大类编号
                wd_days: undefined,  // 平日工作天数
                we_days: undefined,  // 周末工作天数
                wd_hours: undefined,  // 平日工作时长
                we_hours: undefined,  // 周末工作时长
                monday_time: '1',
                tuesday_time: '1',
                wednesday_time: '1',
                thursday_time: '1',
                friday_time: '1',
                saturday_time: '1',
                sunday_time: '1'
            }
        };

        vm.initData = function(){
            if(vm.data.classId){
                // 查看
                vm.getClassType();
            }else {
                // 添加
                vm.data.isCheck = false;
            }
        };

        vm.editor = function(){
            vm.data.isCheck = false;
            $('div.ui.dropdown.disabled').removeClass('disabled');
        };

        vm.getClassType = function(){
            var data = vm.data;
            var queryConfig = {
                funcName: 'getClassType',
                propChain: [''],
                showLoadLayer: false,
                urlPlaceholder: {
                    id: data.classId
                }
            };

            TeacherService.getClassType(queryConfig).then(function(returnedData){
                if(returnedData.success){
                    Object.keys(data.formData).forEach(function(key){
                        this[key] = returnedData.data[key];
                    }, data.formData);

                    // 下拉框 set 值
                    setTimeout(function(){
                        ['monday_time', 'tuesday_time', 'wednesday_time', 'thursday_time', 'friday_time', 'saturday_time',
                                'sunday_time'].forEach(function(key){
                                    $('#' + key).dropdown('set selected', returnedData.data[key]);
                                });
                    });
                }
            }).catch(function(error){
                console.error(error);
            });
        };

        vm.addClassType = function(){
            var postConfig = {
                funcName: 'addClassType',
                successMsg: '添加成功',
                errorMsg: '添加失败',
                showLoadLayer: false,
                httpPostData: vm.data.formData
            };

            TeacherService.addClassType(postConfig).catch(function(error){
                console.error(error);
            });
        };

        vm.updateClassType = function(){
            var data = vm.data;
            var putConfig = {
                funcName: 'updateClassType',
                successMsg: '修改成功',
                errorMsg: '修改失败',
                httpPutData: data.formData,
                showLoadLayer: false,
                urlPlaceholder: {
                    id: data.classId
                }
            };

            TeacherService.updateClassType(putConfig).catch(function(error){
                console.error(error);
            });
        };

        vm.deleteType = function(){
            var deleteConfig = {
                funcName: 'deleteType',
                successMsg: '删除成功',
                errorMsg: '删除失败',
                showLoadLayer: false,
                urlPlaceholder: {
                    id: vm.data.classId
                }
            };

            var layerIndex = layer.confirm('确定要删除?', {
                btn: ['确定','取消']
            }, function(){
                layer.close(layerIndex);
                TeacherService.deleteType(deleteConfig).then(function(returnedData){
                    if(returnedData.success){
                        $state.go('teacher-class-type');
                    }
                }).catch(function(error){
                    console.error(error);
                });
            });


        };

        valiFrom.extenUniqueVali('uniqueShiftCategory');
        $('#add-big-class-form').form({
            fields: {
                class_number: {
                    rules: [{
                        type: 'empty',
                        prompt: '请输入大类编号(必填项)'
                    },{
                        type: 'uniqueShiftCategory',
                        prompt: '已存在该大类编号，请确保大类编号的唯一性'
                    }]
                },
                wd_days: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周一到周五工作天数(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周一到周五工作天数(自然数)'
                    }]
                },
                wd_hours: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周一到周五工作时长(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周一到周五工作时长(自然数)'
                    }]
                },
                we_days: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周末工作天数(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周末工作天数(自然数)'
                    }]
                },
                we_hours: {
                    rules: [{
                        type: 'empty',
                        prompt: '请填写周末工作时长(必填项)'
                    },{
                        type: 'integer[0..]',
                        prompt: '请输入正确的周末工作时长(自然数)'
                    }]
                },
                monday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周一选项(必选项)'
                    }]
                },
                tuesday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周二选项(必选项)'
                    }]
                },
                wednesday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周三选项(必选项)'
                    }]
                },
                thursday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周四选项(必选项)'
                    }]
                },
                friday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周五选项(必选项)'
                    }]
                },
                saturday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周六选项(必选项)'
                    }]
                },
                sunday_time: {
                    rules: [{
                        type: 'empty',
                        prompt: '请选择周日选项(必选项)'
                    }]
                }
            },
            onSuccess: function(){
                console.log('success');
                if(vm.data.classId){
                    vm.updateClassType();
                }else {
                    vm.addClassType();
                }
                return false;
            }
        });
    }
}());
